package com.example.quartz.util;

import com.example.quartz.app.entity.JobEntity;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.TriggerBuilder;

public class Utils {

    public static JobDetail getJobDetail(JobEntity entity){
        try {
            Class clazz = Class.forName(entity.getJobClass());
            return JobBuilder.newJob(clazz).withIdentity(entity.getJobName(), entity.getJobGroup()).build();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static CronTrigger getCronTrigger(JobEntity entity){
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(entity.getJobCron());
        return TriggerBuilder.newTrigger().withIdentity(entity.getTriggerName(), entity.getTriggerGroup())
                .withSchedule(scheduleBuilder).build();
    }
}
